package br.edu.ufabc.lnc.peanho.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class InputReadingsWindow extends JDialog implements ActionListener {

	private final JLabel messageLabel;
	private final JTextField reading1;
	private final JTextField reading2;
	private final JTextField reading3;
	private final JButton okButton;
	private final JButton cancelButton;
	private int[] readings;
	
	public InputReadingsWindow(JFrame owner) {
		super(owner, "Leituras", true);
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setLayout(new GridBagLayout());
		messageLabel = new JLabel("<html>Insira as leituras<br /> de intesidade de sinal:</html>");
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridwidth = 6;
		gbc.gridheight = 2;
		add(messageLabel, gbc);
		JLabel ap1Label = new JLabel("AP1:");
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.gridy = 2;
		add(ap1Label, gbc);
		reading1 = new JTextField(3);
		gbc.gridx = 1;
		add(reading1, gbc);
		JLabel ap2Label = new JLabel("AP2:");
		gbc.gridx = 2;
		add(ap2Label, gbc);
		reading2 = new JTextField(3);
		gbc.gridx = 3;
		add(reading2, gbc);
		JLabel ap3Label = new JLabel("AP3:");
		gbc.gridx = 4;
		add(ap3Label, gbc);
		reading3 = new JTextField(3);
		gbc.gridx = 5;
		add(reading3, gbc);
		okButton = new JButton("Atualizar");
		okButton.addActionListener(this);
		cancelButton = new JButton("Cancelar");
		cancelButton.addActionListener(this);
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.gridwidth = 6;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(buttonPanel, gbc);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		pack();
		getRootPane().setDefaultButton(okButton);
	}
	
	public int[] getReadings() {
		return readings;
	}
	
	public static int[] requestReadings(JFrame owner, int[] readings) {
		InputReadingsWindow dialogWindow = new InputReadingsWindow(owner);
		dialogWindow.readings = readings;
		dialogWindow.reading1.setText(String.valueOf(readings[0]));
		dialogWindow.reading2.setText(String.valueOf(readings[1]));
		dialogWindow.reading3.setText(String.valueOf(readings[2]));
		dialogWindow.setVisible(true);
		return dialogWindow.getReadings();
	}

//	public static void main(String[] args) {
//		JFrame frame = new JFrame("Teste");
//		frame.setPreferredSize(new Dimension(200, 100));
//		int[] readings = InputReadingsWindow.requestReadings(frame);
//		for (int i : readings) {
//			System.out.println(i);
//		}
//	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source.equals(okButton)) {
			try {
				readings[0] = Integer.parseInt(reading1.getText());
				readings[1] = Integer.parseInt(reading2.getText());
				readings[2] = Integer.parseInt(reading3.getText());
				this.dispose();
			} catch (NumberFormatException numberFormatException) {
				JOptionPane.showMessageDialog(this,
						"Número em formato inválido");
			}
		}
		else {
			this.dispose();
		}

	}

}
